package com.javaweb.config.webmvc;

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.javaweb.constant.SystemConstant;
import com.javaweb.constant.SystemConstant.HeadConstant;
import com.javaweb.interceptor.WebPermissionInterceptor;
import com.javaweb.resolver.TokenDataResolver;

/**
 * WebMvc配置
 * <p>
 * //5.3.x+不再需要显式设置了
 * public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
 *     configurer.favorPathExtension(false);
 * }
 * @author tank2140896
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new WebPermissionInterceptor()).addPathPatterns(SystemConstant.WEB_PERMISSION_INTERCEPTOR_PATTERNS);
    }
    
    //更加细化的可以在Controller中写为:@CrossOrigin(origins="http://192.168.1.100:8080",maxAge=3600)
    public void addCorsMappings(CorsRegistry corsRegistry) {
    	corsRegistry.addMapping("/**")
        	        .allowedOriginPatterns("*")//.allowedOrigins("*")//新版不适用了
        	        .allowedHeaders("*")
        	        .allowedMethods("*")
        	        //处理前端拿不到服务器response返回的header里key对应的value值
        	        .exposedHeaders(HeadConstant.RESPONSE_HEADER_URL_PATTERN,HeadConstant.RESPONSE_HEADER_NONCE)
        	        .allowCredentials(true);
    }

    //这里主要是添加资源访问例外(http://IP:端口/项目名称/swagger-ui.html)
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    	//registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
    	//registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
    }

	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
		resolvers.add(new TokenDataResolver());
	}
    
}
